package com.leo;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @ClassName TableViewCustomCellTypeDemo
 * @Description 自定义单元格数据类型
 * @Author wangss
 * @date 2020.06.14 00:10
 * @Version 1.0
 */
public class TableViewCustomCellTypeDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #fff2f2");

        StudentPro s1 = new StudentPro("小a", 10, 50d, false);
        StudentPro s2 = new StudentPro("小b", 11, 65d, true);
        StudentPro s3 = new StudentPro("小c", 11, 74d, true);
        StudentPro s4 = new StudentPro("小d", 9, 100d, true);

        ObservableList<StudentPro> list = FXCollections.observableArrayList();
        list.addAll(s1, s2, s3, s4);

        TableView<StudentPro> tableView = new TableView<>(list);
        tableView.setPrefWidth(400d);
        tableView.setPrefHeight(600d);

        tableView.setFixedCellSize(60d);
        tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        tableView.setEditable(true);

        TableColumn<StudentPro, String> name = new TableColumn<>("姓名");
        name.setCellValueFactory(new PropertyValueFactory<>("name"));
        name.setCellFactory(new Callback<TableColumn<StudentPro, String>, TableCell<StudentPro, String>>() {
            @Override
            public TableCell<StudentPro, String> call(TableColumn<StudentPro, String> param) {
                TableCell<StudentPro, String> tableCell = new MyTableCell<>();
                return tableCell;
            }
        });

        TableColumn<StudentPro, Integer> age = new TableColumn<>("年龄");
        age.setCellValueFactory(new PropertyValueFactory<>("age"));
        age.setCellFactory(new Callback<TableColumn<StudentPro, Integer>, TableCell<StudentPro, Integer>>() {
            @Override
            public TableCell<StudentPro, Integer> call(TableColumn<StudentPro, Integer> param) {
                TableCell<StudentPro, Integer> tableCell = new MyTableCell<>();
                return tableCell;
            }
        });


        TableColumn<StudentPro, Double> score = new TableColumn<>("成绩");
        score.setCellValueFactory(new PropertyValueFactory<>("score"));
        score.setCellFactory(new Callback<TableColumn<StudentPro, Double>, TableCell<StudentPro, Double>>() {
            @Override
            public TableCell<StudentPro, Double> call(TableColumn<StudentPro, Double> param) {
                TableCell<StudentPro, Double> tableCell = new TableCell<StudentPro, Double>() {
                    @Override
                    protected void updateItem(Double item, boolean empty) {
                        super.updateItem(item, empty);

                        if (!empty) {
                            HBox hBox = new HBox();
                            hBox.setAlignment(Pos.CENTER);

                            ProgressIndicator pi = new ProgressIndicator();
                            pi.setProgress(item / 100);
                            hBox.getChildren().addAll(pi);

                            this.setGraphic(hBox);
                        }
                    }
                };
                return tableCell;
            }
        });

        TableColumn<StudentPro, Boolean> flag = new TableColumn<>("是否及格");
        flag.setCellValueFactory(new PropertyValueFactory<>("flag"));
        flag.setCellFactory(new Callback<TableColumn<StudentPro, Boolean>, TableCell<StudentPro, Boolean>>() {
            @Override
            public TableCell<StudentPro, Boolean> call(TableColumn<StudentPro, Boolean> param) {
                TableCell<StudentPro, Boolean> tableCell = new MyTableCell<>();
                return tableCell;
            }
        });

        tableView.getColumns().addAll(name, age, score, flag);

        AnchorPane.setTopAnchor(tableView, 50d);
        AnchorPane.setLeftAnchor(tableView, 50d);
        anchorPane.getChildren().addAll(tableView);

        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.setTitle("TableView");
        primaryStage.setResizable(false);
        primaryStage.show();
    }
}
